package pl.edu.pw.polygen.ui.file;

import java.io.File;

import javax.annotation.PostConstruct;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import pl.edu.pw.polygen.MainWindow;
import pl.edu.pw.polygen.PolygenApplication;
import pl.edu.pw.polygen.beans.FileBean;
import pl.edu.pw.polygen.core.file.FileService;
import pl.edu.pw.polygen.exception.FileExistException;
import pl.edu.pw.polygen.modeler.server.EditorServer;
import pl.edu.pw.polygen.modeler.server.xml.PolygenData;
import pl.edu.pw.polygen.ui.explorer.ProjectExplorer;
import pl.edu.pw.polygen.util.ApplicationHelper;
import pl.edu.pw.polygen.util.ApplicationHolder;

import com.vaadin.ui.AbstractLayout;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Panel;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;

@Component
@Scope("prototype")
public class FileWindow extends Window {

	private static final long serialVersionUID = 3439452761531103410L;
	private static final String windowName = "New File";

	private Logger LOG = Logger.getLogger(FileWindow.class);
	
	@Autowired
	private FileService fileService;
	
	@Autowired
	private FileForm fileForm;
	
	@Autowired
	private EditorPanelFactory editorPanelFactory;
	
	@Autowired
	private ProjectExplorer projectExplorer;
	
	private FileWindow instance;
	private Button okButton;
	private Button cancelButton;
	private HorizontalLayout buttonsLayout;

	public FileWindow() {
		super(windowName);
		instance = this;
	}

	@SuppressWarnings("unused")
	@PostConstruct
	private void initialize() {
		setModal(true);
		setResizable(false);
		VerticalLayout layout = (VerticalLayout) this.getContent();
		layout.setMargin(true);
		layout.setSpacing(true);
		layout.setSizeUndefined();

		layout.addComponent((AbstractLayout) fileForm);
		layout.addComponent(getButtonsLayout());
		layout.setComponentAlignment(getButtonsLayout(), Alignment.BOTTOM_RIGHT);
	}

	private HorizontalLayout getButtonsLayout() {
		if(buttonsLayout==null){
			buttonsLayout = new HorizontalLayout();
			buttonsLayout.setSpacing(true);
			buttonsLayout.addComponent(getOkButton());
			buttonsLayout.addComponent(getCancelButton());
		}
		return buttonsLayout;
	}

	private Button getOkButton() {
		if (okButton == null) {
			okButton = new Button("Save");
			okButton.addListener(new ClickListener() {

				private static final long serialVersionUID = 982663756216569796L;

				@Override
				public void buttonClick(ClickEvent event) {
					if(fileForm.isValid()){
						(instance.getParent()).removeWindow(instance);
						String fileName = fileForm.getFileName();
						String fileDescription = fileForm.getFileDescription();
						Long projectId = fileForm.getProjectId();
						File file = fileForm.getUploadFile();
					
						try{
							FileBean fileBean = fileService.createNewFile(fileName,fileDescription,projectId);
							projectExplorer.addFileBean(fileBean);
							MainWindow mainWindow = (MainWindow) ((PolygenApplication)ApplicationHolder.getApplication()).getMainWindow();
							Panel panel = editorPanelFactory.getPanel();
							VerticalLayout verticalLayout = (VerticalLayout) panel.getContent();
							EditorServer editorServer = (EditorServer) verticalLayout.getComponent(0);
							String content = editorServer.loadFromPolyFile(file);
							fileBean.setContent(content);
							fileService.saveFile(fileBean);
							mainWindow.addTab(fileBean,panel);
							ApplicationHelper.showNotification("Save file", "File '" + fileName +"' has been created successfully.");
						}catch(FileExistException e){
							LOG.error(e.getMessage());
							getWindow().showNotification(e.getMessage());
							ApplicationHelper.showNotification("Save file error", "There was an error while creating a file '" + fileName + "'");
						}
						
					}
				}
			});
			okButton.setWidth("80px");
		}
		return okButton;
	}

	private Button getCancelButton() {
		if (cancelButton == null) {
			cancelButton = new Button("Cancel");
			cancelButton.addListener(new ClickListener() {

				private static final long serialVersionUID = 6840259256005300430L;

				@Override
				public void buttonClick(ClickEvent event) {
					(instance.getParent()).removeWindow(instance);
				}
			});
			cancelButton.setWidth("80px");
		}
		return cancelButton;
	}
	
	public void init(){
		fileForm.initialize();
		fileForm.setFileName("");
		fileForm.setFileDescription("");
	}
	
	public void init(String projectName){
		fileForm.initialize();
		fileForm.setProjectName(projectName);
		fileForm.setFileName("");
		fileForm.setFileDescription("");
	}

}
